package com.Generics.bean.ch04;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author nmm
 * @date 2024-03-2024/3/31
 * @deriction njm......
 */
public class Master <T extends Meekly>{

    private T pet;

    private  String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Master(T pet, String name) {
        this.pet = pet;
        this.name = name;
    }

    public void Shoping() {
        try {
            Class<?> myPetClazz = pet.getClass();//获取class实例

            Field petFieldName = myPetClazz.getDeclaredField("pname");//拿到完整的属性(private java.lang.String com.Generics.annotation.ch01.Dog.pname)
            petFieldName.setAccessible(true);//凡是对公开以外的属性要进行操作时,都要开启可访问为true
            Object petName = petFieldName.get(pet);//通过具体的实例拿到其具体属性值

            Method soundMethod = myPetClazz.getDeclaredMethod("sound",new Class<?>[0]);//通过方法名获取叫的方法
            System.out.println(this.name+"带着"+petName+"在nmm翻斗花园逛街!!!");
            soundMethod.invoke(pet);//调用
        } catch (Exception e) {
            throw  new RuntimeException("此动物没有叫的方法(sound)");
        }
    }

}